open BUFF,"../files/reference/chr23/samples.txt" or die "can't open the file\n";
#$hapmap3 = "/data1/bsi/bioinf_int/s106381.borawork/hapmap3/hapmap3_noID";
$hapmap3 = $ARGV[0];
chomp($hapmap3);
open(BUFF1,"$hapmap3.tfam");
$dir = "../files/reference/chr23";
$malefile = "../files/reference/chr23/male.tfam";
$femalefile = "../files/reference/chr23/female.tfam";
open(WRBUFF1,">$malefile");
open(WRBUFF2,">$femalefile");
#open(WRBUF,">allsamples_list");
$line = <BUFF>;
@array = split(" ",$line);
#print "@array\n"; 
shift(@array);
shift(@array);
while($line = <BUFF1>)
{
	chomp($line);
	#print $line."\n";
	@arr =split(" ",$line);
	#print $arr[4]."\n";
	$hash{$arr[1]} = $arr[4];
	$hash1{$arr[1]} = $line;

}
$S_male = "";
$S_female = "";
$km =1;
for($i=0;$i<@array;$i++)
{
	#if(exists($hash1{$array[$i]}) && $km%2 == 0)
	#{
#		print WRBUF $hash1{$array[$i]}."\n";
#	}
	if($hash{$array[$i]} == 1)
	{
		if($km%2 == 0)
		{
			print WRBUFF1 $hash1{$array[$i]}."\n";
			$S_male = $S_male." $array[$i]";
		}
	}
	elsif($hash{$array[$i]} == 2)
	{
		if($km%2 == 0)
		{
			print WRBUFF2 $hash1{$array[$i]}."\n";
		}
		$S_female = $S_female." $array[$i]";
	}
	else
	{

	}
	$km++;	
}
#print $S_male."\n".$S_female."\n";
#system("./plink/plink --tfile $hapmap3 --chr 23 --keep $malefile --transpose --recode --out $dir/male");
#system("./plink/plink --tfile $hapmap3 --chr 23 --keep $femalefile --transpose --recode --out $dir/female");
open(MBUFF,"$dir/male.tped");
open(FBUFF,"$dir/female.tped");
open(MWRBUFF,">$dir/phasedmale.bgl");
open(FWRBUFF,">$dir/phasedfemale.bgl");
open(WRBUFF,">$dir/markers.txt");
print MWRBUFF "I id$S_male\n";
print FWRBUFF "I id$S_female\n";
while($line = <MBUFF>)
{
	chomp($line);
	@array = split(" ",$line);
	shift(@array);
	$rsid =  shift(@array);
	shift(@array);
	$pos = shift(@array); 
	print MWRBUFF "M $rsid";
		
	for($i=0;$i<@array;$i++)
	{
		print MWRBUFF " $array[$i]";
		$i++;
	} 	
	print MWRBUFF "\n";
	$kk =0;
	if($line =~ m/A/)
	{
		$ar[$kk] ="A";
		$kk++;	
	}
	if($line =~ m/C/)
        {
                $ar[$kk] ="C";
                $kk++;
        }
	if($line =~ m/G/)
        {
                $ar[$kk] ="G";
                $kk++;
        }
	if($line =~ m/T/)
        {
                $ar[$kk] ="T";
                $kk++;
        }
	$hash1{$rsid} =join(" ",@ar);
}
while($line = <FBUFF>)
{
        chomp($line);
        @array = split(" ",$line);
        shift(@array);
        $rsid =  shift(@array);
        shift(@array);
        $pos = shift(@array);
        print FWRBUFF "M $rsid";

        for($i=0;$i<@array;$i++)
        {
                print FWRBUFF " $array[$i]";
        }
        print FWRBUFF "\n";
	$line = $line.$hash1{$rsid};
	$kk =0;
        if($line =~ m/A/)
        {
                $ar[$kk] ="A";
                $kk++;
        }
        if($line =~ m/C/)
        {
                $ar[$kk] ="C";
                $kk++;
        }
        if($line =~ m/G/)
        {
                $ar[$kk] ="G";
                $kk++;
        }
        if($line =~ m/T/)
        {
                $ar[$kk] ="T";
                $kk++;
        }
	if(@ar ==1)
	{
		print WRBUFF $rsid." ".$pos." ".$ar[0]." ".$ar[0]."\n";	
	}
	else
	{
		print WRBUFF $rsid." ".$pos." ".$ar[0]." ".$ar[1]."\n";
	}
}

